#include "SList.h"
//申请结点
SLNode* BuyNode(SLDateType x)
{
    SLNode* newNode = (SLNode*)malloc(sizeof(SLNode));
    if(newNode == NULL)
    {
        perror("malloc error!\n");
        exit(1);
    }
    newNode->date = x;
    newNode->next = NULL;
    return newNode;
}
//打印链表
void SListPrint(SLNode** pphead)
{
   assert(pphead);
   SLNode* ptail = *pphead;
   while(ptail)
   {
        printf("%d->",ptail->date);
        ptail = ptail->next;
   }
   printf("NLLL\n");
}
//单链表的尾插
void SListPushBack(SLNode** pphead,SLDateType x)
{
    assert(pphead);
    SLNode* newnode = BuyNode(x);
    if(*pphead == NULL)
    {
        *pphead = newnode;
    }
    else
    {
        SLNode* ptail = *pphead;
        while(ptail->next)
        {
            ptail = ptail->next;
        }
        ptail->next = newnode;
    }
}
//链表的销毁
void SListDestroy(SLNode** pphead)
{
    assert(pphead);
    SLNode* ptail = *pphead;
    SLNode* pcur = NULL;
    while(ptail)
    {
        pcur = ptail->next;
        free(ptail);
        ptail = pcur;
    }
    *pphead = NULL;
}
//单链表的头插
void SListPushFront(SLNode** pphead,SLDateType x)
{
    assert(pphead);
    SLNode* newnode = BuyNode(x);
    SLNode* ptail = *pphead;
    newnode->next = ptail;
    *pphead = newnode;
}
//单链表的尾删
void SlistPopBack(SLNode** pphead)
{
    assert(pphead && *pphead);
    if((*pphead)->next == NULL)
    {
        free(*pphead);
        *pphead = NULL;
    }
    else
    {
        SLNode* ptail = *pphead;
        SLNode* pcur = NULL;
        while(ptail->next)
        {
            pcur = ptail;
            ptail = ptail->next;
        }
        free(ptail);
        ptail = NULL;
        pcur->next = NULL;
    }

}
//单链表的头删
void SListPopFront(SLNode** pphead)
{
    assert(pphead && *pphead);
    SLNode* ptail = *pphead;
    (*pphead) = (*pphead)->next;
    free(ptail);
    ptail = NULL;
}
//单链表的查找
SLNode* SListFind(SLNode** pphead,SLDateType x)
{
    assert(pphead && *pphead);
    SLNode* ptail = *pphead;
    while(ptail)
    {
        if(ptail->date == x) return ptail;
        ptail = ptail->next;
    }
    return NULL;
}
//在指定位置之前插入数据
void SListInsert(SLNode** pphead,SLNode* pos,SLDateType x)
{
    assert(pphead && pos);
    SLNode* newnode = BuyNode(x);
    if(*pphead == pos)
    {
        SListPushFront(pphead,x);
    }
    else
    {
        SLNode* ptail = *pphead;
        while(ptail->next != pos)
            ptail = ptail->next;
        ptail->next = newnode;
        newnode->next = pos;
    }
}
//删除指定位置的结点
void SListErase(SLNode** pphead,SLNode* pos)
{
    assert(pphead && *pphead && pos);
    if(*pphead == pos)
    {
        *pphead = (*pphead)->next;
    }
    else
    {
        SLNode* ptail = *pphead;
        while(ptail->next != pos)
            ptail = ptail->next;
        SLNode* next = pos->next;
        ptail->next = next;
    }
    free(pos);
    pos = NULL;
}
//在指定位置之后插入数据
void SListInsertAfert(SLNode* pos,SLDateType x)
{
    assert(pos);
    SLNode* newnode = BuyNode(x);
    SLNode* next = pos->next;
    pos->next = newnode;
    newnode->next = next;
}
//删除指定位置之后的节点
void SListEraseAfert(SLNode* pos)
{
    assert(pos);
    if(pos ->next != NULL)
    {
        SLNode* next = pos->next->next;
        SLNode* del = pos->next;
        pos->next = next;
        free(del);
        del = NULL;
    }
}